const app = getApp()
// https://blog.csdn.net/weixin_43625081/article/details/107320240
Page({
  data: {
    active: 0, //活跃状态
    isOpenBLE: false, //是否打开蓝牙
    status: [{ //统一
        module: '单色模式',
        light: 10,
        code: 1,
        R: 255,
        G: 0,
        B: 0
      },
      { //分区一
        module: '单色模式',
        light: 10,
        code: 1,
        R: 255,
        G: 0,
        B: 0
      },
      { // 分区二
        module: '单色模式',
        light: 10,
        code: 1,
        R: 255,
        G: 0,
        B: 0
      }
    ],
    //模式
    lightModule: [{
        module: '关闭',
        code: 0
      }, {
        module: '单色模式',
        code: 1
      },
      {
        module: '呼吸模式',
        code: 2
      },
      {
        module: '渐变模式',
        code: 3
      },
      {
        module: '爆闪模式',
        code: 4,
      },
      {
        module: '律动模式',
        code: 5,
      }
    ],
    light: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] //灯亮度
  },
  onLoad() {},

  onShow() {
    // this.openBEL()
  },

  // 切换首页
  otherIndex(){
    wx.reLaunch({
      url: '/pages/control/control',
    })
  },

  //分区切换事件事件
  onChangeActive(e) {
    let val = e.detail.index;
    app.Toast(e.detail.title)
    this.setData({
      active: val
    })
  },



  //主分区选中颜色
  pickColor(e) {
    if (app.globalData.deviceId) {
      let color = JSON.parse(e.detail);
      const {
        red,
        green,
        blue
      } = color;
      this.setData({
        [`status[0].module`]: '单色模式',
        [`status[0].code`]: 1,
        [`status[0].R`]: red,
        [`status[0].G`]: green,
        [`status[0].B`]: blue,
        [`status[1].module`]: '单色模式',
        [`status[1].code`]: 1,
        [`status[2].module`]: '单色模式',
        [`status[2].code`]: 1,
      })
      let status = this.data.status[0];
      app.writeBLE(0, status.light, status.code, status.R, status.G, status.B)
    }
  },
  //分区一选中颜色
  pickColorOne(e) {
    if (app.globalData.deviceId) {
      let color = JSON.parse(e.detail);
      const {
        red,
        green,
        blue
      } = color;
      this.setData({
        [`status[1].module`]: '单色模式',
        [`status[1].code`]: 1,
        [`status[1].R`]: red,
        [`status[1].G`]: green,
        [`status[1].B`]: blue,
      })
      let status = this.data.status[1];
      app.writeBLE(1, status.light, status.code, status.R, status.G, status.B)
    }

  },
  //分区二选中颜色
  pickColorTwo(e) {
    if (app.globalData.deviceId) {
      let color = JSON.parse(e.detail);
      const {
        red,
        green,
        blue
      } = color;
      this.setData({
        [`status[2].module`]: '单色模式',
        [`status[2].code`]: 1,
        [`status[2].R`]: red,
        [`status[2].G`]: green,
        [`status[2].B`]: blue,
      })
      let status = this.data.status[2];
      app.writeBLE(2, status.light, status.code, status.R, status.G, status.B)
    }

  },




  //灯光模式
  onLightModule(e) {
    let index = e.detail.value;
    let val = this.data.lightModule[index];
    let active = this.data.active;
    let status = this.data.status[active];
    this.setData({
      [`status[${active}].module`]: val.module,
      [`status[${active}].code`]: val.code
    })


    if (active == 0) { //主分区修改模式，分区一和分区二也要跟着改
      this.setData({
        [`status[1].module`]: val.module,
        [`status[1].code`]: val.code,
        [`status[2].module`]: val.module,
        [`status[2].code`]: val.code
      })
    }
    app.writeBLE(active, status.light, status.code, status.R, status.G, status.B)
  },



  //亮度改变
  onLight(e) {
    let index = Number(e.detail.value) + 1;
    let active = this.data.active;
    let status = this.data.status[active];
    this.setData({
      [`status[0].light`]: index,
      [`status[1].light`]: index,
      [`status[2].light`]: index,
    })
    console.log(status);
    app.writeBLE(active, status.light, status.code, status.R, status.G, status.B)
  },









  

  //是否为苹果手机
  isApple() {
    let systemInfo = wx.getSystemInfoSync();
    //手机系统是否为ios
    return systemInfo.system.indexOf('iOS') != -1 ? true : false
  },
  //分享好友
  onShareAppMessage() {

  },
})